/**
 * 封装http 请求方法
 */
// const appUrl = 'https://wx.xtwaixie.com/api/';
// const img_path = 'https://wx.xtwaixie.com';
import {
	feedbackError
} from "@/utils/util"
const url = 'https://wx.xtwaixie.com'; //正服
// const url = 'https://xitengtest.xtwaixie.com'; //测服' 
export const appUrl = url + '/api/';
const img_path = 'https://cdn.xtwaixie.com';
let callback = null
const request = (api, params, method) => {
	let s = uni.getStorageSync('token') || '';
	//返回promise 对象
	return new Promise((resolve, reject) => {
		let h = {
			'content-type': 'application/json'
		};
		if (s) {
			h.token = s;
		}
		var task = uni.request({
			url: appUrl + api,
			//服务器url+参数中携带的接口具体地址
			data: params || {},
			//请求参数
			method: method || 'POST',
			header: h,
			success: (res) => {
				//接口访问正常返回数据
				if (res.statusCode == 200) {
					if (res.data.code == 200) {
						// feedbackError({custom_data:params,message:'测试'})
						resolve(res.data);
						return;
					} else if (res.data.code == 203 || res.data.code == 204 || res.data.code ==
						205) {
						//清除用户信息
						uni.removeStorageSync('userInfo');
						uni.removeStorageSync('token');
						if (callback) return
						callback = setTimeout(() => {
							const pages = getCurrentPages();
							const currentPage = pages[pages.length - 1];
							if (currentPage != 'pages/login/login') {
								clearTimeout(callback)
								callback = null
								uni.navigateTo({
									url: '/pages/login/login'
								});
							}
						}, 1000);
					} else if (res.data.code == 207) {
						uni.showToast({
							title: '用户已冻结！',
							icon: 'none',
							duration: 2000
						});
						uni.navigateTo({
							url: '/pages/frozen/frozen'
						});
					} else {
						console.log('111');
						// uni.showToast({
						// 	title: api + 'code异常',
						// 	icon: 'none'
						// });
						feedbackError({custom_data:params,message:JSON.stringify(res.data)})
						reject(res.data);
					}
				} else {
					//2. 操作不成功返回数据，以toast方式弹出响应信息，如后端未格式化非操作成功异常信息，则可以统一定义异常提示
					console.log('返回状态码非200');
					console.log(res.data);
					// uni.showToast({
					// 	title: api + 'statusCode非200',
					// 	icon: 'none'
					// });
					feedbackError({custom_data:params,message:JSON.stringify(res.data)})
					reject(res.data);
				}
			},
			fail: function(e) {
				// uni.showToast({
				// 	title: api + '调用出错',
				// 	icon: 'none'
				// });
				feedbackError({custom_data:params,message:JSON.stringify(e)})
				console.log('返回失败');
				reject(e);
			}
		});
		task.onHeadersReceived((header) => {});
	});
};
const upLoadFile = (api, name, filePath, params = {}) => {
	let s = uni.getStorageSync('token') || '';
	//返回promise 对象
	return new Promise((resolve, reject) => {
		let h = {
			'content-type': 'application/x-www-form-urlencoded',
			token: s
		};
		uni.uploadFile({
			url: appUrl + api,
			filePath: filePath,
			header: h,
			name: name,
			formData: params,
			success: function(res) {
				let data = JSON.parse(res.data);
				if (data.code == 200) {
					resolve(data);
				}
				reject(data);
			},
			fail: function(e) {
				reject(e);
			}
		});
	});
};
module.exports = {
	request,
	upLoadFile,
	url,
	appUrl,
	img_path
};